﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using Tau.Model;
using Tau.Service;
using Tau.ViewModels;
using Tau.Web.Infrastructure;

namespace Tau.Web.Areas.Admin.Controllers
{
    public class ImageAdminController : BaseAdminController
    {
        public ActionResult Index()
        {
            //Get all images to display
            var svc = new TauService();

            var images = svc.GetAllImages();

            return View("Index", new ImageList { Images = images, Mode = DisplayMode.Edit});
        }

        public ActionResult ImageList(ImageList imageList)
        {
            return PartialView("ImageList", imageList);
        }

        public ActionResult ImageList()
        {
            //Get all images to display
            var svc = new TauService();
            List<Image> images = svc.GetAllImages();

            return PartialView("ImageList", new ImageList { Images = images, Mode = DisplayMode.View, Category = images[0].Category});
        }

        public ActionResult ImagesByCategory(string category, string mode)
        {
            //Get all images to display
            var svc = new TauService();
            List<Image> images = new List<Image>();

            if (string.IsNullOrEmpty(category))
            {
                images = svc.GetAllImages();
            }
            else
            {
                images = svc.GetImagesByCategory(category);
            }

            return PartialView("ImageList", new ImageList { Images = images, Mode = (DisplayMode)Enum.Parse(typeof(DisplayMode), mode), Category = category });
        }

        public ActionResult AddNewImage()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddNewImage(AddImage addImage)
        {
            var svc = new TauService();

            var uploadedFile = Request.Files[0];

            if (uploadedFile != null)
            {
                using (var binaryReader = new BinaryReader(uploadedFile.InputStream))
                {
                    addImage.AddingImage.LargeImage = binaryReader.ReadBytes(uploadedFile.ContentLength);
                    addImage.AddingImage.FileName = uploadedFile.FileName;
                    addImage.AddingImage.Size = uploadedFile.ContentLength;
                }

                int largeImageWidth = Convert.ToInt32(ConfigurationManager.AppSettings["LargeImageWidth"]);
                int thumbnailImageWidth = Convert.ToInt32(ConfigurationManager.AppSettings["ThumbnailImageWidth"]);

                svc.AddImage(addImage.AddingImage, largeImageWidth, thumbnailImageWidth);
            }

            var images = svc.GetAllImages();

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id, DisplayMode mode)
        {
            var svc = new TauService();

            try
            {
                svc.DeleteImage(id);
            }
            catch
            {
            }
           
            var images = svc.GetAllImages();

            return PartialView("ImageList", new ImageList { Images = images, Mode = mode });
        }

        public ActionResult ChangeImageCategory(int id, string newCategory, string category)
        {
            var svc = new TauService();

            svc.UpdateImage(id, newCategory);

            return ImagesByCategory(category, DisplayMode.Edit.ToString());
        }
    }
}
